﻿/***********************************************************************************************
 * 
 * ProjectName: WMSSoft.Lib.WinKStart
 * 
 * Description: Class to apply Styles to GUI
 * 
 * Created:     29.08.2014
 * 
 * Author:      matthias wagler - matthias@WMSSoft.de
 * 
 * Copyright:   Copyright 2014 by WMSSoft.net
 * 
 * License:     GPLv3
 * 
 * History:     29.08.2014 - matthias wagler matthias@WMSSoft.de   -   Created
 * 
 **********************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.Const;
using WMSSoft.Lib.Enums;

namespace WMSSoft.Lib.WinKStart
{
    public static class cStyleChanger
    {
        /// <summary>
        /// Apply Style to Main Window
        /// </summary>
        public static void ApplyStyleToWindow(System.Windows.Window RenderForm)
        {
            try
            {
                if (RenderForm != null)
                {
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIWindow oWindowStyle = cUserSettingsReader.StyleReader.GetWindowStyle();
                    if (oWindowStyle != null)
                    {
                        //Set Background style
                        switch (oWindowStyle.DrawingMode)
                        {
                            case eDrawingMode.SingleColor:
                                RenderForm.Background = new System.Windows.Media.SolidColorBrush(oWindowStyle.BackgroundColorStart);
                                break;
                            case eDrawingMode.Gradient:
                                RenderForm.Background = new System.Windows.Media.LinearGradientBrush(oWindowStyle.BackgroundColorStart,
                                                                                                     oWindowStyle.BackgroundColorEnd,
                                                                                                     oWindowStyle.Gradient.Angle);
                                break;
                        }
                        //set Border style
                        if (oWindowStyle.BorderStyle.Visible == true && oWindowStyle.BorderStyle.Width > 0)
                        {
                            RenderForm.BorderBrush = new System.Windows.Media.SolidColorBrush(oWindowStyle.BorderStyle.Color);
                            RenderForm.BorderThickness = new System.Windows.Thickness(oWindowStyle.BorderStyle.Width);
                        }
                        //Set Padding
                        RenderForm.Padding = oWindowStyle.Padding;
                        RenderForm.Height = oWindowStyle.Height;
                        RenderForm.Width = oWindowStyle.Width;
                    }

                }
            }
            catch (Exception Ex)
            {
                throw new Exception(LanguageStrings.GetText(Texte.ErrorMessages.STYLE_CHANGER__ERROR_APPLYING_WINDOW_STYLE), Ex);
            }
        }

        /// <summary>
        /// Apply Style to a WPF - Image - Object
        /// </summary>
        /// <param name="Picture">Object to Style with</param>
        /// <param name="StyleElement">Elements Name</param>
        public static void ApplyStyleToImage(System.Windows.Controls.Image Picture, string StyleElement, WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementImage StylePAram = null)
        {
            try
            {
                if (Picture != null && ((StyleElement.Length > 0) | (StyleElement.Length == 0 && StylePAram != null)))
                {
                    //Read Style - Element
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementImage oImageStyle = null;                    

                    if (StylePAram != null)
                        oImageStyle = StylePAram;
                    else
                        oImageStyle = cUserSettingsReader.StyleReader.GetImageStyle(StyleElement); ;

                    if (oImageStyle != null)
                    {
                        string sImagePath = "";
                        //Set Picture Path
                        if (oImageStyle.Source.Length > 0)
                        {
                            sImagePath = System.IO.Path.Combine(cUserSettingsReader.StylesPath, 
                                                                cUserSettingsReader.StyleReader.StyleSettings[StyleElements.STYLE_SETTING__STYLE_FOLDER]);

                            sImagePath = System.IO.Path.Combine(sImagePath, oImageStyle.Source);                            
                        }
                        if (sImagePath.Length > 0)
                        {
                            System.Windows.Media.Imaging.BitmapImage bitImage = new System.Windows.Media.Imaging.BitmapImage(new Uri(sImagePath));
                            Picture.Width = oImageStyle.Width;
                            Picture.Height = oImageStyle.Height;
                            Picture.Stretch = oImageStyle.Stretch;
                            Picture.StretchDirection = oImageStyle.StretchDirection;
                            Picture.Source = bitImage;
                            Picture.Margin = oImageStyle.Margin;
                            Picture.Visibility = oImageStyle.Visible;
                        }

                        Picture.Margin = oImageStyle.Margin;
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception(LanguageStrings.GetText(Texte.ErrorMessages.STYLE_CHANGER__ERROR_APPLYING_PICTURE_STYLE), Ex);
            }

        }

        public static void ApplyStyleToElements(System.Windows.Controls.Control Element, string StyleElement, WMSSoft.Lib.WinKStart.Classes.Styles.cUIElement StyleParam = null)
        {
            try
            {
                if (Element != null && ((StyleElement.Length > 0) | (StyleElement.Length == 0 && StyleParam != null)))
                {
                    //Read Style - Element
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElement oElementStyle = null;
                    if (StyleParam != null)
                        oElementStyle = StyleParam;
                    else 
                        oElementStyle = cUserSettingsReader.StyleReader.GeteElementStyle(StyleElement);

                    if (oElementStyle != null)
                    {
                        //Set Background Style
                        Element.Background = new System.Windows.Media.SolidColorBrush(oElementStyle.BackgroundColor);

                        Element.FontFamily = new System.Windows.Media.FontFamily(oElementStyle.FontFamily);
                        Element.FontSize = oElementStyle.FontSize;
                        Element.Foreground = new System.Windows.Media.SolidColorBrush(oElementStyle.FontColor);
                        Element.FontStyle = oElementStyle.FontStyle;
                        Element.FontWeight = oElementStyle.FontWeight;
                        Element.HorizontalContentAlignment = ConverToHorizAlignment(oElementStyle.HorizontalAlign);
                        Element.VerticalContentAlignment = ConvertToVertAlignment(oElementStyle.VerticalAlign);
                        Element.Padding = oElementStyle.Padding;
                        Element.Margin = oElementStyle.Margin;

                        if (oElementStyle.Height > -1)
                            Element.Height = oElementStyle.Height;
                        if (oElementStyle.Width > -1)
                            Element.Width = oElementStyle.Width;
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception(LanguageStrings.GetText(Texte.ErrorMessages.STYLE_CHANGER__ERROR_APPLYING_ELEMENT_STYLE), Ex);
            }
        }

        public static void ApplyStyleToRegionPosition(System.Windows.Controls.Panel Element)
        {
            try
            {
                if (Element != null)
                {
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementRegionPosition oRegionPosition = cUserSettingsReader.StyleReader.GetRegionPositionStyle();
                    if (oRegionPosition != null)
                    {
                        Element.Height = oRegionPosition.Height;
                        Element.Margin = oRegionPosition.Margin;
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception(LanguageStrings.GetText(Texte.ErrorMessages.STYLE_CHANGER__ERROR_APPLYING_REGION_POSITION_STYLE), Ex);
            }
        }

        public static void ApplyStyleToPanel(System.Windows.Controls.Control Element, string Stylename)
        {
            try
            {
                if (Element != null)
                {
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementPanel oPanel = cUserSettingsReader.StyleReader.GetPanelStyle(Stylename);
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementHeader oPanelHeader = oPanel.Header;

                    if (oPanel != null && oPanelHeader != null)
                    {
                        Element.Background = new System.Windows.Media.SolidColorBrush(oPanel.BackgroundColor);
                        Element.Padding = new System.Windows.Thickness();
                        Element.Margin = new System.Windows.Thickness();
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception(LanguageStrings.GetText(Texte.ErrorMessages.STYLE_CHANGER__ERROR_APPLYING_PANEL_STYLE), Ex);
            }
        }

        public static void ApplyStyleToPanelHeader(System.Windows.Controls.Label Element, string StyleElement)
        {
            try
            {
                if (Element != null)
                {
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementPanel oPanel = cUserSettingsReader.StyleReader.GetPanelStyle(StyleElement);
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementHeader oPanelHeader = oPanel.Header;

                    if (oPanelHeader != null)
                    {
                        Element.Background = new System.Windows.Media.SolidColorBrush(oPanelHeader.BackgroundColor);
                        Element.FontFamily = new System.Windows.Media.FontFamily(oPanelHeader.FontFamily);
                        Element.Foreground = new System.Windows.Media.SolidColorBrush(oPanelHeader.FontColor);
                        Element.FontSize = oPanelHeader.FontSize;
                        Element.FontStyle = oPanelHeader.FontStyle;
                        Element.FontWeight = oPanelHeader.FontWeight;
                        Element.Height = oPanelHeader.Height;
                        Element.VerticalContentAlignment = ConvertToVertAlignment(oPanelHeader.VerticalAlign);
                        Element.HorizontalContentAlignment = ConverToHorizAlignment(oPanelHeader.HorizontalAlign);
                        Element.Padding = new System.Windows.Thickness();
                        Element.Margin = oPanelHeader.Margin;
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception(LanguageStrings.GetText(Texte.ErrorMessages.STYLE_CHANGER__ERROR_APPLYING_PANEL_STYLE), Ex);
            }
        }

        public static void ApplyStyleToPanelData(System.Windows.Controls.ListBox Element, string StyleElement)
        {
            try
            {
                if (Element != null)
                {
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementPanel oPanel = cUserSettingsReader.StyleReader.GetPanelStyle(StyleElement);
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementData oRegionDAta = oPanel.Data;

                    if (oRegionDAta != null)
                    {
                        Element.Background = new System.Windows.Media.SolidColorBrush(oRegionDAta.BackgroundColor);
                        Element.FontFamily = new System.Windows.Media.FontFamily(oRegionDAta.FontFamily);
                        Element.Foreground = new System.Windows.Media.SolidColorBrush(oRegionDAta.FontColor);
                        Element.FontSize = oRegionDAta.FontSize;
                        Element.FontStyle = oRegionDAta.FontStyle;
                        Element.FontWeight = oRegionDAta.FontWeight;
                        //Element.Height = oRegionDAta.Height;
                        Element.VerticalContentAlignment = ConvertToVertAlignment(oRegionDAta.VerticalAlign);
                        Element.Padding = oRegionDAta.Padding;
                        Element.Margin = new System.Windows.Thickness();
                        //style line height

                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception(LanguageStrings.GetText(Texte.ErrorMessages.STYLE_CHANGER__ERROR_APPLYING_PANEL_STYLE), Ex);
            }
        }

        public static void ApplyStyleToRegion(System.Windows.Controls.Panel Element, string Stylename)
        {
            try
            {
                if (Element != null)
                {
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementRegion oRegion = cUserSettingsReader.StyleReader.GetRegionStyle(Stylename);
                    if (oRegion != null)
                    {
                        Element.Background = new System.Windows.Media.SolidColorBrush(oRegion.BackgroundColor);
                        Element.Margin = new System.Windows.Thickness();
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception(LanguageStrings.GetText(Texte.ErrorMessages.STYLE_CHANGER__ERROR_APPLYING_REGION_STYLE), Ex);
            }
        }

        public static void ApplyStyleToRegionHeader(System.Windows.Controls.Control Element, string StyleElement)
        {
            try
            {
                if (Element != null )
                {
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementRegion oRegion = cUserSettingsReader.StyleReader.GetRegionStyle(StyleElement);
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementHeader oRegionHeader = oRegion.Header;

                    if (oRegionHeader != null)
                    {
                        Element.Background = new System.Windows.Media.SolidColorBrush(oRegionHeader.BackgroundColor);
                        Element.FontFamily = new System.Windows.Media.FontFamily(oRegionHeader.FontFamily);
                        Element.Foreground = new System.Windows.Media.SolidColorBrush(oRegionHeader.FontColor);
                        Element.FontSize = oRegionHeader.FontSize;
                        Element.FontStyle = oRegionHeader.FontStyle;
                        Element.FontWeight = oRegionHeader.FontWeight;
                        Element.Height = oRegionHeader.Height;
                        Element.VerticalContentAlignment = ConvertToVertAlignment(oRegionHeader.VerticalAlign);
                        Element.HorizontalContentAlignment = ConverToHorizAlignment(oRegionHeader.HorizontalAlign);
                        Element.Padding = new System.Windows.Thickness();
                        Element.Margin = oRegionHeader.Margin;
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception(LanguageStrings.GetText(Texte.ErrorMessages.STYLE_CHANGER__ERROR_APPLYING_REGION_STYLE), Ex);
            }
        }

        public static void ApplyStyleToRegionBreadCrumb(System.Windows.Controls.Control Element, string StyleElement)
        {
            try
            {
                if (Element != null)
                {
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementRegion oRegion = cUserSettingsReader.StyleReader.GetRegionStyle(StyleElement);
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementHeader oRegionHeader = oRegion.Header;

                    if (oRegionHeader != null)
                    {
                        Element.Background = new System.Windows.Media.SolidColorBrush(oRegionHeader.BackgroundColor);
                        Element.FontFamily = new System.Windows.Media.FontFamily(oRegionHeader.FontFamily);
                        Element.Foreground = new System.Windows.Media.SolidColorBrush(oRegionHeader.FontColor);
                        Element.FontSize = oRegionHeader.FontSize;
                        Element.FontStyle = oRegionHeader.FontStyle;
                        Element.FontWeight = oRegionHeader.FontWeight;
                        Element.Height = oRegionHeader.Height;
                        Element.VerticalContentAlignment = ConvertToVertAlignment(oRegionHeader.VerticalAlign);
                        //ConverToHorizAlignment(oRegionHeader.HorizontalAlign);
                        Element.Padding = oRegionHeader.Padding;
                        Element.Margin = new System.Windows.Thickness();
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception(LanguageStrings.GetText(Texte.ErrorMessages.STYLE_CHANGER__ERROR_APPLYING_REGION_STYLE), Ex);
            }
        }

        public static void ApplyStyleToRegionData(System.Windows.Controls.ListBox Element, string StyleElement)
        {
            try
            {
                if (Element != null)
                {
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementRegion oRegion = cUserSettingsReader.StyleReader.GetRegionStyle(StyleElement);
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementData oRegionDAta = oRegion.Data;

                    if (oRegionDAta != null)
                    {
                        Element.Background = new System.Windows.Media.SolidColorBrush(oRegionDAta.BackgroundColor);
                        Element.FontFamily = new System.Windows.Media.FontFamily(oRegionDAta.FontFamily);
                        Element.Foreground = new System.Windows.Media.SolidColorBrush(oRegionDAta.FontColor);
                        Element.FontSize = oRegionDAta.FontSize;
                        Element.FontStyle = oRegionDAta.FontStyle;
                        Element.FontWeight = oRegionDAta.FontWeight;
                        //Element.Height = oRegionDAta.Height;
                        Element.VerticalContentAlignment = ConvertToVertAlignment(oRegionDAta.VerticalAlign);
                        Element.Padding = oRegionDAta.Padding;
                        Element.Margin = new System.Windows.Thickness();
                        //style line height
                        
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception(LanguageStrings.GetText(Texte.ErrorMessages.STYLE_CHANGER__ERROR_APPLYING_REGION_STYLE), Ex);
            }
        }

        public static void ApplyStyleToRegionButtons(System.Windows.Controls.Panel Element)
        {
            try
            {
                if (Element != null)
                {
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementRegionButtons oRegionButtons = cUserSettingsReader.StyleReader.GetRegionButtonsStyle();
                    if (oRegionButtons != null)
                    {
                        Element.Margin = oRegionButtons.Margin;
                        Element.Height = oRegionButtons.Height;
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception(LanguageStrings.GetText(Texte.ErrorMessages.STYLE_CHANGER__ERROR_APPLYING_REGION_BUTTONS_STYLE), Ex);
            }
        }

        public static void ApplyStyleToREgionButton(System.Windows.Controls.StackPanel Element, string RegionID, bool Active)
        {
            try
            {
                if (Element != null)
                {
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementRegionButton oRegionButton = cUserSettingsReader.StyleReader.GetRegionButtonStyle(RegionID);
                    WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementRegionButtons oRegionButtons = cUserSettingsReader.StyleReader.GetRegionButtonsStyle();

                    if (oRegionButton != null && oRegionButtons != null)
                    {
 
                        //Element.Margin = new System.Windows.Thickness();
                        Element.Height = oRegionButtons.Height;
                        Element.Width = oRegionButton.Width;

                        //Apply Style to Buttons Label
                        int iLabelIndex = 1;
                        if (Element.Children.Count == 1)
                            iLabelIndex = 0;

                        if (Element.Children.Count > 1)
                        {
                            cStyleChanger.ApplyStyleToImage(((System.Windows.Controls.Image)Element.Children[0]), "", oRegionButton.Image);
                        }
                        cStyleChanger.ApplyStyleToElements(((System.Windows.Controls.Label)Element.Children[iLabelIndex]), "", oRegionButton.Label);
                    }
                }
            }
            catch (Exception Ex)
            {
                throw new Exception(LanguageStrings.GetText(Texte.ErrorMessages.STYLE_CHANGER__ERROR_APPLYING_REGION_BUTTONS_STYLE), Ex);
            }
        }

        public static int GetPanelDataLineHeight(string ElementID)
        {
            try
            {
                WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementPanel oPanel = cUserSettingsReader.StyleReader.GetPanelStyle(ElementID);                
                WMSSoft.Lib.WinKStart.Classes.Styles.cUIElementData oPanelData = oPanel.Data;

                return oPanelData.LineHeight;
                
            }
            catch (Exception Ex)
            {
                throw new Exception("Can't get data lineheight!", Ex);
            }
        }

        private static System.Windows.HorizontalAlignment ConverToHorizAlignment(eXMLHorizontalAlign Align)
        {
            switch (Align)
            {
                case eXMLHorizontalAlign.Left:
                    return System.Windows.HorizontalAlignment.Left;
                case eXMLHorizontalAlign.Center:
                    return System.Windows.HorizontalAlignment.Center;
                case eXMLHorizontalAlign.Right:
                    return System.Windows.HorizontalAlignment.Right;
                case eXMLHorizontalAlign.Justify:
                    return System.Windows.HorizontalAlignment.Stretch;
                default:
                    return System.Windows.HorizontalAlignment.Left;
            }
        }

        private static System.Windows.VerticalAlignment ConvertToVertAlignment(eXMLVerticalAlign Align)
        {
            switch (Align)
            {
                case eXMLVerticalAlign.Top:
                    return System.Windows.VerticalAlignment.Top;
                case eXMLVerticalAlign.Center:
                    return System.Windows.VerticalAlignment.Center;
                case eXMLVerticalAlign.Bottom:
                    return System.Windows.VerticalAlignment.Bottom;
                default:
                    return System.Windows.VerticalAlignment.Top;
            }
        }
    }
}
